package com.lagou.user.controller;

import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lagou.user.bean.Token;
import com.lagou.user.bean.User;
import com.lagou.user.dao.TokenRepository;
import com.lagou.user.dao.UserRepository;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    UserRepository userRepository;

    @Autowired
    TokenRepository tokenRepository;

    @GetMapping("/{email}/{password}")
    public String login(@PathVariable("email") String email, @PathVariable("password") String password){
        User user = userRepository.findByEmail(email);
        if(user == null || !StringUtils.equals(user.getPassword(), password)){
            return "false";
        }
        String uuid = UUID.randomUUID().toString();
        Token token = new Token();
        token.setEmail(email);
        token.setToken(uuid);
        tokenRepository.deleteByEmail(email);
        tokenRepository.save(token);
        return uuid;
    }

    @GetMapping("/{email}")
    public boolean login(@PathVariable("email") String email){
        return true;
    }


}
